package com.booleam.service.impl;

import com.booleam.dao.UserDAO;
import com.booleam.model.ResponseDO;
import com.booleam.model.UserDO;
import com.booleam.service.LoginService;
import com.booleam.utils.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author asen
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserDAO userDAO;

    @Override
    public ResponseDO login(String username, String password) throws Exception {
        ResponseDO result;
        String psw = userDAO.queryPasswordByUserName(username);
        if(psw.equalsIgnoreCase(password)) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESS, null);
        } else {
            result = new ResponseDO(ResultCode.USER_PASSWORD_ERROR, null);
        }
        return result;
    }

    @Override
    public ResponseDO register(String username, String password) throws Exception {
        ResponseDO result;

        UserDO user = new UserDO();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(2);

        int count = userDAO.insertUserDO(user);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESS, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }
        return result;
    }
}
